<template>
  <div class="paper-detail">
    <el-card>
      <template #header>
        <div class="card-header">
          <h3>试卷详情</h3>
          <div class="header-actions">
            <el-button type="primary" @click="handleEdit">编辑试卷</el-button>
            <el-button @click="$router.back()">返回</el-button>
          </div>
        </div>
      </template>

      <div v-loading="loading">
        <!-- 基本信息 -->
        <div class="basic-info">
          <h4>基本信息</h4>
          <el-descriptions :column="2" border>
            <el-descriptions-item label="试卷名称">{{
              paperInfo.paperName
            }}</el-descriptions-item>
            <el-descriptions-item label="建议用时">{{ paperInfo.duration }}分钟</el-descriptions-item>
            <el-descriptions-item label="题目数量">{{
              paperInfo.questionCount
            }}</el-descriptions-item>
            <el-descriptions-item label="总分">{{
              paperInfo.totalScore
            }}</el-descriptions-item>
            <el-descriptions-item label="创建时间">{{
              paperInfo.createdTime
            }}</el-descriptions-item>
            <!-- <el-descriptions-item label="创建者">{{
              paperInfo.creator
            }}</el-descriptions-item> -->
            <el-descriptions-item label="试卷说明" :span="2">
              {{ paperInfo.description || "暂无说明" }}
            </el-descriptions-item>
          </el-descriptions>
        </div>

        <!-- 题目列表 -->
        <div class="question-list">
          <h4>题目列表</h4>
          <el-table :data="questionList" style="width: 100%" border @row-click="handleViewQuestion">
            <el-table-column prop="index" min-width="80" label="序号"  sortable/>
            <el-table-column prop="typeId" label="题型" min-width="85" sortable>
              <template #default="{ row }">
                <el-tag :type="getTypeTag(row.typeId)">{{
                  getTypeText(row.typeId)
                }}</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="content" label="题目内容" min-width="300" show-overflow-tooltip sortable/>
            <el-table-column prop="score" label="分值" min-width="100" sortable/>
            <el-table-column prop="difficulty" label="难度" min-width="150" sortable>
              <template #default="{ row }">
                <el-rate v-model="row.difficulty" disabled :max="5" />
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </el-card>

    <!-- 题目详情对话框 -->
    <QuestionPreView ref="questionPreviewRef"/>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { useRoute, useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import { paperApi } from "../../api/paperApi";
import { getTypeTag, getTypeText } from "../../utils/utils";
import QuestionPreView from "../Question/QuestionPreView.vue";

const route = useRoute();
const router = useRouter();
const loading = ref(false);
const paperInfo = ref({});
const questionList = ref([]);
const questionPreviewRef = ref(null);

// 获取试卷详情
const getPaperDetail = async () => {
  loading.value = true;
  try {
    const res = await paperApi.getPaperDetail(route.params.id);
    paperInfo.value = res.result;
    if(res.result.paperQuestions) {
      questionList.value = res.result.paperQuestions;
      questionList.value.forEach((question, index) => {
        question.index = index + 1;
      });
    }
  } catch (error) {
    console.error("获取试卷详情失败:", error);
    ElMessage.error("获取试卷详情失败");
  } finally {
    loading.value = false;
  }
};

// 编辑试卷
const handleEdit = () => {
  router.push(`/paper/${route.params.id}/edit`);
};

// 查看题目详情
const handleViewQuestion = (question) => {
  questionPreviewRef.value.handlePreview(question);
};

onMounted(() => {
  getPaperDetail();
});
</script>

<style scoped>
.paper-detail {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-actions {
  display: flex;
  gap: 10px;
}

.basic-info {
  margin:0 200px 30px;
}

.basic-info h4,
.question-list h4 {
  margin-bottom: 15px;
  font-weight: 500;
}

.question-list {
  margin:0 200px 30px;
}

</style>
